<?php
class Mail extends AppModel {
    var $useTable = false;
    var $_schema = array(
        'email_method'  => array('type'=>'string', 'length'=>255),
        'email_contact'	=> array('type'=>'string', 'length'=>255), 
        'email_sender'	=> array('type'=>'string', 'length'=>255), 
        'smtp_host'	=> array('type'=>'string', 'length'=>255),
        'smtp_port'	=> array('type'=>'string', 'length'=>255), 
        'smtp_username'	=> array('type'=>'string', 'length'=>255), 
        'smtp_password'	=> array('type'=>'string', 'length'=>255),
    );
    public $validate = array(
        'email_contact' => array(
            'notEmpty' => array(
                'rule'    => 'notEmpty',
            ),
            'email' => array(
                'rule'    => 'email',
            ),
        ),
        'email_sender' => array(
            'notEmpty' => array(
                'rule'    => 'notEmpty',
            )
        ),
    );
    
    /**
     * 
     */
    public function validateSmtp(){
        $this->validate['smtp_host'] = array(
            'notEmpty' => array(
                'rule'    => 'notEmpty',
            )
        );
        $this->validate['smtp_port'] = array(
            'notEmpty' => array(
                'rule'    => 'notEmpty',
            ),
            'alphaNumeric' => array(
                'rule'    => 'alphaNumeric',
            ),
        );
        $this->validate['smtp_username'] = array(
            'notEmpty' => array(
                'rule'    => 'notEmpty',
            )
        );
        $this->validate['smtp_password'] = array(
            'notEmpty' => array(
                'rule'    => 'notEmpty',
            )
        );
    }
    
    /**
     * 
     */
    public function getSettingMail(){
        App::import('model', 'Setting');
        $ob = new Setting();
        $result['Mail'] = array(
            'email_method'	=> $ob->getSettingValue("email_method"),
            'email_contact'	=> $ob->getSettingValue("email_contact"), 
            'email_sender'	=> $ob->getSettingValue("email_sender"),
            'smtp_host'         => $ob->getSettingValue("smtp_host"),
            'smtp_port'         => $ob->getSettingValue("smtp_port"),
            'smtp_username'	=> $ob->getSettingValue("smtp_username"),
            'smtp_password'	=> $ob->getSettingValue("smtp_password"),
        );
        return $result;
    }
    
    /**
     *
     * @param type $data 
     */
    public function saveSetting($data){
        
        App::import('model', 'Setting');
        $ob = new Setting();
        foreach($data['Mail'] as $key => $value){
            $ob->create();
            $ob->saveSetting(array('name' => $key,'value' => $value));
        }
    }
}